<?php
/**
 * 设备控制器
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         EquipmentController.php
 */
namespace Scshux\Front\Controllers;
use Scshux\Utils\JSON;
use Scshux\Mvc\SModel;
use Scshux\Utils\Uploader;
use Scshux\Mvc\SQuery;
use Scshux\Plugin\Wechat;
use Scshux\Utils\Helper;
use Scshux\Tag\SmartyTag;
use Scshux\Utils\Tree;
class EquipmentController extends  ControllerBase
{
	/**
	 * 产品列表
	 */
	public  function  indexAction()
	{
		$cid = intval($_REQUEST['cid']);
		$page = intval($_REQUEST['_page']);
		if(empty($page))$page =1;

		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();

		//查找顶层分类
		if($site_id ==2){
			$parentID = 40;
			if(!$cid) $cid = 40;
		}else{
			$parentID = 35;
			if(!$cid) $cid = 35;
		}

		//读取当前分类
		$goodsCategoryModel = new SModel('category');
		$category = $goodsCategoryModel->getObj('id='.$cid);
		if(empty($category)){
			$this->redirect('');
		}

		//读取所有分类
		$goodsCategoryModel->setFetchMode(\PDO::FETCH_ASSOC);
		$goodsCategories = $goodsCategoryModel->query('site_id='.$site_id,'*','sort desc,id','desc');
		$treeObj = new Tree($goodsCategories);

		//读取当前语言下所有的分类
		$childCategories = $treeObj->getTree('child',$parentID);

		/**
		 * 读取当前id的分类
		*/
		$childIds = $treeObj->getChilds($cid);
		$childIds[] = $cid;


		//读取产品
		$goodsQuery = new SQuery('goods as g');
		$goodsQuery->join = ' join category as c on g.cat_id=c.id ';
		$goodsQuery->fields = 'g.goods_id,g.cat_id,g.goods_name,g.goods_img,c.name as cat_name';
		$goodsQuery->where = 'g.cat_id in('.join(',', $childIds).')' .' and g.site_id='.$site_id;;
		$goodsQuery->page = $page;
		$goodsQuery->pagesize = 6;
		$goodsItems = $goodsQuery->find();

		if($category->id != $parentID){//不要产品中心分类
			$this->view->setVar('currentCategory', $category);
		}

		$this->view->setVar('pager', $goodsQuery->paging->getPager());
		$this->view->setVar('goodsItems', $goodsItems);
		$this->view->setVar('categories', $childCategories);
		$this->view->setVar('currentCid',$cid);
		$this->view->setVar('seo_title', $category->name);

	}

	/**
	 * 产品展示
	 */
	public  function  showAction()
	{
		$id = $this->request->get('id','int',0);
		if(empty($id)){
			$this->redirect('');
		}
		$id = intval($id);
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();

		//根据语言判断分类id
		if($site_id ==2){
			$parentID = 40;
		}else{
			$parentID = 35;
		}

		//读取产品
		$goodsQuery = new SQuery('goods as g');
		$goodsQuery->join = ' join category as c on g.cat_id=c.id ';
		$goodsQuery->fields = 'g.*,c.name as cat_name';
		$goodsQuery->where = 'g.goods_id='.$id .' and g.site_id='.$site_id;

		$goodsItems = $goodsQuery->find();

		if(empty($goodsItems)){
			$this->redirect('');
		}

		//读取所有分类
		$goodsCategoryModel = new SModel('category');
		$goodsCategoryModel->setFetchMode(\PDO::FETCH_ASSOC);
		$goodsCategories = $goodsCategoryModel->query('site_id='.$site_id,'*','sort desc,id','desc');
		$treeObj = new Tree($goodsCategories);

		$childCategories = $treeObj->getTree('child',$parentID);

		$childIds = $treeObj->getChilds($goodsItems[0]->cat_id);
		$childIds[] = intval($goodsItems[0]->cat_id);

		//读取图片
		$goodsGalleryModel = new SModel('goods_gallery');
		$goodsGalleries = $goodsGalleryModel->query('goods_id='.$id);

		//读取同级产品
		$goodsQuery = new SQuery('goods as g');
		$goodsQuery->join = ' join category as c on g.cat_id=c.id ';
		$goodsQuery->fields = 'g.goods_id,g.cat_id,g.goods_name,g.goods_img,c.name as cat_name';
		$goodsQuery->where = 'g.cat_id in('.join(',', $childIds).') and g.goods_id!='.$id .' and g.site_id='.$site_id;;
		$goodsQuery->limit = 6;

		//更新次数
		$goodsModel = new SModel('goods');
		$goodsModel->updateBySql('goods_id='.$id, 'click_count = click_count+1');


		$this->view->setVar('relatedItems', $goodsQuery->find());
		$this->view->setVar('categories', $childCategories);
		$this->view->setVar('goodsGalleries', $goodsGalleries);
		$this->view->setVar('item', $goodsItems[0]);
		$this->view->setVar('seo_title', $goodsItems[0]->goods_name);
		$this->view->setVar('seo_keywords', $goodsItems[0]->keywords);
		$this->view->setVar('currentCid',$cid);
	}
	
	
	/**
	 * 设计师团队
	 */
	public function designerAction()
	{
	    $cid = intval($_REQUEST['cid']);
	    
	    $page = intval($_REQUEST['_page']);
	    
	    $teamQuery = new SQuery('designer');
	    $teamQuery->order = 'id desc';
	    $teamQuery->page = $page;
	    $teamQuery->pagesize = 9;
	    
	    if ($cid)
	    {
	        $teamQuery->where = 'type='.$cid;
	    }
	    $items = $teamQuery->find();
	    
	    if ($items)
	    {
	        $caseModel = new SModel('cases');
	        foreach ($items as $k => $item)
	        {
	            $cases = $caseModel->query('design_id='.$item->id,'name');
	            if ($cases)
	            {
	                $caseName = array();
	                foreach ($cases as $case)
	                {
	                    $caseName []= $case->name;
	                }
	                $items[$k]->casenames = join(',', $caseName);
	            }
	        }
	    }
	    $this->view->setVar('pager', $teamQuery->paging->getPager());
	    $this->view->setVar('items', $items);
	    $this->view->setVar('cid',$cid);
	    
	}
	
	/**
	 * 设计师详情
	 */
	public function designerinfoAction()
	{
	    $id = intval($_GET['id']);
	    
	    $designModel = new SModel('designer');
	    
	    $designer = $designModel->getObj('id='.$id);
	    
	    if (!$designer)
	    {
	        return $this->redirect('errors/err404');
	    }
	    
	    $casesQuery = new SQuery('cases as cs');
	    $casesQuery->where = 'cs.design_id='.$id;
	    $casesQuery->fields = 'cs.id,cs.name,cs.area,cs.img,s.style_name';
	    $casesQuery->join = ' left join goods_style as s on s.id=cs.style ';
	    $casesQuery->order = 'cs.id desc';
	    $casesQuery->limit = 4;
	    
	    $cases = $casesQuery->find();
	    
	    $this->view->setVar('item', $designer);
	    
	    $this->view->setVar('cases',$cases);
	    
	}
	
	
	
}